<?php


namespace app\admin\controller\sergood;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="服务产品列表")
 */
class ServiceGoods extends AdminController
{

    use \app\admin\traits\Curd;
    protected $sort = [
        'id'   => 'desc',
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new \app\admin\model\ServiceGoods();
        
    }
    
    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
          
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
 

    /**
    * @NodeAnotation(title="添加")
    */
    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            if(empty($post['title'])){
                $this->error('服务名称不能为空');
            }
            $data = [
                'title'=>$post['title'],
                'zhu_img'=>$post['image'],
                'price'=>$post['price'],
                'is_online'=>$post['is_online'],
                'create_time'=>time()
            ];
            try {
                $this->model->save($data);
            } catch (\Exception $e) {
                $this->error('保存失败:' . $e->getMessage());
            }
            $this->success('保存成功');
        }else{
            return $this->fetch();
        }
        
    }

     /**
    * @NodeAnotation(title="编辑")
    */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            if(empty($post['title'])){
                $this->error('服务名称不能为空');
            }
            $data = [
                'title'=>$post['title'],
                'zhu_img'=>$post['image'],
                'price'=>$post['price'],
                'is_online'=>$post['is_online'],
            ];
            try {
               $this->model->where('id',$post['id'])->update($data);
             
            } catch (\Exception $e) {
                $this->error('保存失败:' . $e->getMessage());
            }
            $this->success('保存成功') ;
        }else{
            $post = $this->request->param(); 
            $row = $this->model->where('id',$post['id'])->find();
            $this->assign('row', $row); 
            return $this->fetch();
        }
       
    }
    

     public function upload(){
        $file = $_FILES['file'];
     
        if (!isset($file['tmp_name']) || !$file['tmp_name']) {
            echo json_encode(array('code' => 401, 'msg' => '没有文件上传'));
            return false;
        }
        if ($file["error"] > 0) {
            echo json_encode(array('code' => 402, 'msg' => $file["error"]));
            return false;
        }
      
        $filename = time().rand(0, 1000);
        $ext = pathinfo($_FILES['file']['name'])['extension'];
        $filename = $filename . '.' . $ext;
        $upload_path = "uploads/";
        
        $domain = ($this->isHTTPS() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST']; //获取域名
        $save_path =$domain.'/' . $upload_path . $filename;
        if (!is_dir($upload_path)) {
            mkdir(iconv("UTF-8", "UTF-8", $upload_path), 0777, true);
        }
        if (move_uploaded_file($file["tmp_name"], $upload_path . $filename)) {
            echo json_encode(array('code' => 200, 'src' => $save_path));
        } else {
            echo json_encode(array('code' => 404, 'msg' => '上传失败'));
            return false;
        }
          
    }


    
 
} 